using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.WarehouseReserveManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using Yitter.IdGenerator;

namespace Business.WarehouseReserveManagement
{
    [Authorize(BusinessPermissions.WarehouseReserve.Default)]
    public class WarehouseReserveAppService : ApplicationService,IWarehouseReserveAppService
    {
        private const string FormName = "WarehouseReserve";
        private IRepository<WarehouseReserve, Guid> _repository;

        public WarehouseReserveAppService(
            IRepository<WarehouseReserve, Guid> repository
            )
        {
            _repository = repository;
        }
        #region 增删改查基础方法

        public async Task<WarehouseReserveDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<WarehouseReserve, WarehouseReserveDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<WarehouseReserveDto>> GetAll(GetWarehouseReserveInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync());
            var query = (await _repository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrWhiteSpace(input.DocumentCode), a => a.DocumentCode.Contains(input.DocumentCode))
                .WhereIf(!string.IsNullOrWhiteSpace(input.ExternalNumber), a => a.ExternalNumber.Contains(input.ExternalNumber))
                .WhereIf(!string.IsNullOrWhiteSpace(input.WaybillNumber), a => a.WaybillNumber.Contains(input.WaybillNumber))
                .WhereIf(!string.IsNullOrWhiteSpace(input.ShippingCarrier), a => a.ShippingCarrier.Contains(input.ShippingCarrier))
                .WhereIf(!string.IsNullOrWhiteSpace(input.WarehouseLocation), a => a.WarehouseLocation.Contains(input.WarehouseLocation))
                .WhereIf(input.WarehouseType > 0, a => a.WarehouseType == input.WarehouseType)
                .WhereIf(input.WarehouseStatus > 0, a => a.WarehouseStatus == input.WarehouseStatus)
                .WhereIf((!string.IsNullOrWhiteSpace(input.BeginTime) ||!string.IsNullOrWhiteSpace(input.EndTime)), a => a.WarehouseTime >=Convert.ToDateTime (input.BeginTime) && a.WarehouseTime <= Convert.ToDateTime(input.EndTime));
                
            var totalCount = await query.CountAsync();
            var items = await query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount)
                       .Take(input.MaxResultCount)
                       .ToListAsync();

            var dto = ObjectMapper.Map<List<WarehouseReserve>, List<WarehouseReserveDto>>(items);
            return new PagedResultDto<WarehouseReserveDto>(totalCount, dto);
        }

        public async Task<WarehouseReserveDto> DataPost(CreateOrUpdateWarehouseReserveDto input)
        {
            WarehouseReserve result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                input.WarehouseReserveId=GuidGenerator.Create();
                input.GoodId = GuidGenerator.Create();
                
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateWarehouseReserveDto, WarehouseReserve>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<WarehouseReserve, WarehouseReserveDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

     
        #endregion

    }
}